﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirControlATV.Core;
using AirControlATV.Model;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Input;

namespace AirControlATV.Controls
{
    public class AirControlButton : Button
    {
        private static SynchronizationContext _syncContext;

        private AirControlCommand _airControlCommand = null;
        private AirControlCommandType _airControlCommandType;
        private Timer _repeatTimer;
        private bool _hasRunLongPressCommand = false;

        public delegate void CommandResultEventHandler(string resultText);
        public event CommandResultEventHandler CommandResult;

        public AirControlButton()
        {
            _repeatTimer = new Timer(RepeatTimer_Tick, null, Timeout.Infinite, Timeout.Infinite);
        }

        private void RepeatTimer_Tick(object state)
        {
            if (_airControlCommand.IsRepeatableCommand)
                _airControlCommand.RunCommand();
            else
            {
                _airControlCommand.RunLongPressCommand();
                _hasRunLongPressCommand = true;
            }
        }

        public AirControlCommandType AirControlCommandType
        {
            get { return _airControlCommandType; }
            set
            {
                _airControlCommandType = value;

                _airControlCommand = new AirControlCommand(value);

                _airControlCommand.CommandResult += text =>
                {
                    if (CommandResult != null)
                    {
                        _syncContext.Post(taskObject =>
                        {
                            string str = (string)taskObject;
                            CommandResult(str);
                        }, text);
                    }
                };

                Content = _airControlCommand.UserFriendlyText;
            }
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            e.Handled = true;

            if (_syncContext == null)
                _syncContext = SynchronizationContext.Current;

            if (string.IsNullOrEmpty(_airControlCommand.LongPressCommandText))
            {
                _airControlCommand.RunCommand();

                if (_airControlCommand.IsRepeatableCommand)
                    _repeatTimer.Change(300, 100);
            }
            else
            {
                _hasRunLongPressCommand = false;
                _repeatTimer.Change(500, Timeout.Infinite);
            }
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            EndRepeating();
        }

        private void EndRepeating()
        {
            _repeatTimer.Change(Timeout.Infinite, Timeout.Infinite);

            if (string.IsNullOrEmpty(_airControlCommand.LongPressCommandText) == false && _hasRunLongPressCommand == false)
                _airControlCommand.RunCommand();
        }
    }
}
